/**
 * Course			:	ISYS2119 - BIS Development 2
 * Assignment		:	03
 * Date due			:	31/08/2012
 * Program 			:	Saigon Bike Rental Catalogue System
 * 
 * Programmer		:	Vuu Ngoc My Linh - s3342015, Tran Thi Van Anh - s3342025
 * 
 * Attributions		:	Lecture slides, Blackboard, Tutorials, Websites (not in reference list document)
 * ====================================================================
 * Description: Functions of buttons on the forms 						
 */

package rmit.ISYS2119.assign03;

import java.util.*;

public class AccessoryCatalogue {

	// Declare variables
	private ArrayList<Accessory> accessories;
	private accessoryDAO dataReader;
	private String accessoryFileName;

	/**
	 * @param accessoryFileName
	 */
	public AccessoryCatalogue(String bikeFileName) {

		try {
			accessories = new ArrayList<Accessory>();
			dataReader = new accessoryDAO(accessoryFileName);
			setAccessoryFileName(accessoryFileName);
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * @return the accessories
	 */
	public ArrayList<Accessory> getAccessories() {
		return accessories;
	}

	/**
	 * @param accessories
	 *            the accessories to set
	 */
	public void setAccessories(ArrayList<Accessory> accessories) {
		this.accessories = accessories;
	}

	/**
	 * @return the dataReader
	 */
	public accessoryDAO getDataReader() {
		return dataReader;
	}

	/**
	 * @param dataReader
	 *            the dataReader to set
	 */
	public void setDataReader(accessoryDAO dataReader) {
		this.dataReader = dataReader;
	}

	/**
	 * @return the AccessoryFileName
	 */
	public String getAccessoryFileName() {
		return accessoryFileName;
	}

	/**
	 * @param AccessoryFileName
	 *            the AccessoryFileName to set
	 */
	public void setAccessoryFileName(String accessoryFileName) {
		this.accessoryFileName = accessoryFileName;
	}

	public void addAccessory(Accessory accessory) throws Exception {
		accessories.add(accessory);
	}

	public Accessory getAccessory(int index) {
		return accessories.get(index);
	}

	// Delete accessory
	public boolean deleteAccessory(Accessory accessory) {

		boolean deleted = false;

		for (int i = 0; i < accessories.size(); i++) {
			if (accessories.equals(accessories.get(i))) {
				accessories.remove(i);
				deleted = true;
				break;
			}
		}
		return deleted;
	}

	
	public Accessory searchByAccessoryID(int AccessoryId) {

		Accessory accessory = null;

		for (int i = 0; i < accessories.size(); i++) {
			if (AccessoryId == accessories.get(i).getAccessoryId()) {
				accessory = accessories.get(i);
				break;
			}
		}

		return accessory;

	}

	public int size() {
		return accessories.size();
	}

	/**
	 * Test BikeCatalogue Class
	 */
	public static void main(String[] args) {
		
		// Test AccessoryCatalogue Class
		System.out.println("AccessoryCatalogue Class is OK");
	}

}
